package sapling.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sapling.dao.UserDao;
import sapling.form.AdminForm;
import sapling.service.UserService;

import javax.validation.Valid;
import java.util.Objects;

/**
 * Created by Administrator on 2017/6/27.
 */
@Controller
@RequestMapping("/admin/appAdmin")
public class AppAdminAction {

    @Autowired
    private UserDao userDao;
    @Autowired
    private UserService userService;

    @RequestMapping("")
    public String appUser(Model model){
        model.addAttribute("user",userDao.findAll());
        return "admin/appadmin/appAdmin_list";
    }

    @RequestMapping("/save")
    public String save(AdminForm adminForm){
        userService.save(adminForm.getUsername(),adminForm.getPassword());
        return "redirect:/admin/appAdmin";
    }

    @RequestMapping("/create")
    public String create(){
        return "admin/appadmin/appAdmin_create";
    }

    @RequestMapping("/delete")
    public String delete(Long user_id){
        if(Objects.nonNull(user_id)){
            userDao.delete(user_id);
        }
        return "redirect:/admin/appAdmin";
    }

}
